package com.blb.friendsO.dao.impl;

import com.blb.friendsO.dao.IFriendsDao;
import com.blb.friendsO.entity.TComment;
import com.blb.friendsO.entity.TFriends;
import com.blb.friendsO.entity.TGood;
import com.blb.friendsO.utils.JDBCUtils;
import org.springframework.stereotype.Repository;

import javax.xml.stream.events.Comment;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.SQLException;

@Repository
public class FriendsDaoImpl implements IFriendsDao {

    //获取所有动态
    @Override
    public List<TFriends> getFriends() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        List<TFriends> list = JDBCUtils.getList(connection, TFriends.class, "SELECT t1.id, user_id, `name`, img, text,imgs, time   " +
                "  FROM t_friends t1  " +
                "  LEFT JOIN t_user t2 ON t1.user_id = t2.id  " +
                "  ORDER BY time DESC");
        connection.close();
        return list;
    }

    //获得所有点赞
    @Override
    public List<TGood> getGood() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        List<TGood> list = JDBCUtils.getList(connection, TGood.class, "SELECT t1.id id, friends_id, `name` userName FROM t_good t1  " +
                "  LEFT JOIN t_user t2 ON t1.user_id = t2.id");
        connection.close();
        return list;
    }

    //获得所有评论
    @Override
    public List<TComment> getComment() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        List<TComment> list = JDBCUtils.getList(connection, TComment.class, "SELECT t1.id id, friends_id, user_id, t2.`name` userName, t3.`name` addUserName, comment_text  " +
                "  FROM t_comment t1  " +
                "  LEFT JOIN t_user t2 ON t1.user_id = t2.id  " +
                "  LEFT JOIN t_user t3 ON t1.add_user_id = t3.id");
        connection.close();
        return list;
    }


    @Override
    public void add(TFriends fr) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "insert into t_friends(user_id,text,imgs,time) VALUES (?,?,?,?)", fr.getUserId(), fr.getText(), fr.getImgs(), fr.getTime()).executeUpdate();
        connection.close();
    }

    // 删除动态
    @Override
    public void deleteById(int id) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "delete from t_friends where id = ?", id).executeUpdate();
        connection.close();
    }

    @Override
    public void addGood(int userId, int friendsID) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "insert into t_good(user_id,friends_id) VALUES( ?, ?)",userId,friendsID).executeUpdate();
        connection.close();
    }

    @Override
    public int selectGood(int userId, int friendsID) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        ResultSet resultSet = JDBCUtils.prepareStatement(connection, "SELECT count(*)'cou' FROM `t_good` where user_id =? and friends_id =?", userId, friendsID).executeQuery();
        resultSet.next();
        int cou = resultSet.getInt("cou");
        connection.close();
        return cou;
    }

    @Override
    public void deleteGood(int userId, int friendsID) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        int i = JDBCUtils.prepareStatement(connection, "delete from t_good where  user_id =? and friends_id =?",userId,friendsID).executeUpdate();
        connection.close();
    }
}
